package com.apexedu.eas.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.apexedu.eas.service.MsgService;
import com.linkage.netmsg.server.AnswerBean;
import com.linkage.netmsg.server.ReceiveMsg;
import com.linkage.netmsg.server.ReturnMsgBean;

@Component
public class ReceiveMsgImpl extends ReceiveMsg {
	@Autowired
	private MsgService msgService;

	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	private SimpleDateFormat sdf2=new SimpleDateFormat("yyMMddHHmm");

	// query and send
	/**
	 * 接收下行短信返回状态
	 */
	public void getAnswer(AnswerBean answerBean) {
		// TODO Auto-generated method stub
		super.getAnswer(answerBean);
		// 序列ID
		String seqIdString = answerBean.getSeqId();
		// 短信状态
		Integer status = answerBean.getStatus();
		// 下行短信ID
		String msgId = answerBean.getMsgId();
		MsgServiceImpl.map2.put(msgId, MsgServiceImpl.map.get(seqIdString));
//		System.out.println(seqIdString);
//		System.out.println(status);
//		System.out.println(msgId);
		// MsgServiceImpl.map.put(seqIdString, status==0?1:2);
		if (status == 0) {
			Integer row = this.msgService
					.updateStatus(1, sdf.format(new Date()),
							MsgServiceImpl.map.get(seqIdString));
		}
	}

	/**
	 * 接收下行短信回执
	 */
	public void getReturnMsg(ReturnMsgBean returnMsgBean) {
		// TODO Auto-generated method stub
		super.getReturnMsg(returnMsgBean);
		String sequenceId = returnMsgBean.getSequenceId();
		/* 短信的msgId */
		String msgId = returnMsgBean.getMsgId();
		/* 发送号码 */
		String sendNum = returnMsgBean.getSendNum();
		/* 接收号码 */
		String receiveNum = returnMsgBean.getReceiveNum();
		/* 短信提交时间 */
		String submitTime = returnMsgBean.getSubmitTime();
		/* 短信下发时间 */
		String sendTime = returnMsgBean.getSendTime();
		/* 短信状态 */
		String msgStatus = returnMsgBean.getMsgStatus();
		/* 短信错误代码 */
		int msgErrStatus = returnMsgBean.getMsgErrStatus();
//		System.out.println(msgStatus.equals("DELIVRD")?2:-1);
//		try {
//			System.out.println(sdf.format(sdf2.parse(sendTime)));
//		} catch (ParseException e1) {
//			// TODO Auto-generated catch block
//			e1.printStackTrace();
//		}
//		System.out.println(MsgServiceImpl.map2.get(msgId));
		System.out.println("短信的错误代码："+msgErrStatus);
		try {
			Integer row=this.msgService.updateStatus(msgStatus.equals("DELIVRD")?2:-1, sdf.format(sdf2.parse(sendTime)), MsgServiceImpl.map2.get(msgId));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
//		// 此处加入接收短信回执的处理代码
//		System.out.println("ReturnMsgBean sequenceId: " + sequenceId);
//		System.out.println("ReturnMsgBean msgId: " + msgId);
//		System.out.println("ReturnMsgBean sendNum: " + sendNum);
//		System.out.println("ReturnMsgBean receiveNum: " + receiveNum);
//		System.out.println("ReturnMsgBean submitTime: " + submitTime);
//		System.out.println("ReturnMsgBean sendTime: " + sendTime);
//		System.out.println("ReturnMsgBean msgStatus: " + msgStatus);
//		System.out.println("ReturnMsgBean msgErrStatus: " + msgErrStatus);
	}
}
